<?php

namespace app\admin\model\park;

use think\Model;
use traits\model\SoftDelete;

class ParkingPlace extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'parking_place';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'lock_status_text',
        'car_status_text',
        'status_text',
        'online_status_text',
        'start_charge_time_text',
        'end_charge_time_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getLockStatusList()
    {
        return ['close' => __('Lock_status close'), 'closing' => __('Lock_status closing'), 'open' => __('Lock_status open'), 'openning' => __('Lock_status openning'), 'error' => __('Lock_status error')];
    }

    public function getCarStatusList()
    {
        return ['0' => __('Car_status 0'), '1' => __('Car_status 1')];
    }

    public function getStatusList()
    {
        return ['idle' => __('Status idle'), 'charging' => __('Status charging'), 'wait' => __('Status wait')];
    }

    public function getOnlineStatusList()
    {
        return ['online' => __('Online_status online'), 'offline' => __('Online_status offline')];
    }


    public function getLockStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['lock_status']) ? $data['lock_status'] : '');
        $list = $this->getLockStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCarStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['car_status']) ? $data['car_status'] : '');
        $list = $this->getCarStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOnlineStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['online_status']) ? $data['online_status'] : '');
        $list = $this->getOnlineStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStartChargeTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_charge_time']) ? $data['start_charge_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndChargeTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_charge_time']) ? $data['end_charge_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStartChargeTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndChargeTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
